
package com.google.code.sms.android.util;

import com.google.code.sms.android.activity.DownloadActivity;
import com.google.code.sms.android.domain.MediaElement;
import com.google.code.sms.android.provider.SMSAppWidgetProvider;
import com.google.code.sms.android.service.DownloadServiceImpl;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.support.v4.app.NotificationCompat;
import android.view.KeyEvent;
import android.widget.RemoteViews;
import com.google.code.sms.android.R;

public final class NotificationUtil {

	private static final String TAG = NotificationUtil.class.getSimpleName();

    public static void updateNotification(final Context context, final DownloadServiceImpl downloadService,
            Handler handler, MediaElement.Entry song, boolean playing) {

        // On older platforms, show a notification without buttons.
        if (useSimpleNotification()) {
            updateSimpleNotification(context, downloadService, handler, song, playing);
        } else {
            updateCustomNotification(context, downloadService, handler, song, playing);
        }

        // Update widget
        SMSAppWidgetProvider.getInstance().notifyChange(context, downloadService, playing);
    }

    private static void updateSimpleNotification(Context context, final DownloadServiceImpl downloadService, Handler handler,
            MediaElement.Entry song, boolean playing) {

        if (song == null || !playing) {
            hideNotification(downloadService, handler);
        } else {
            final Notification notification = createSimpleNotification(context, song);

            // Send the notification and put the service in the foreground.
            handler.post(new Runnable() 
            {
                public void run() {
                    downloadService.startForeground(Constants.NOTIFICATION_ID_PLAYING, notification);
                }
            });
        }
    }

    private static void updateCustomNotification(Context context, final DownloadServiceImpl downloadService,
            Handler handler, MediaElement.Entry song, boolean playing) {

        if (song == null) {
            hideNotification(downloadService, handler);

        } else if (!isNotificationHiddenByUser(context)) {
            final Notification notification = createCustomNotification(context, song, playing);

            // Send the notification and put the service in the foreground.
            handler.post(new Runnable() 
            {
                public void run() {
                    downloadService.startForeground(Constants.NOTIFICATION_ID_PLAYING, notification);
                }
            });
        }
    }

    public static void hideNotification(final DownloadServiceImpl downloadService, Handler handler) {

        // Remove notification and remove the service from the foreground
        handler.post(new Runnable() 
        {
            public void run() {
                downloadService.stopForeground(true);
            }
        });
    }

    public static void setNotificationHiddenByUser(Context context, boolean hiddenByUser) {
        SharedPreferences preferences = Util.getPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(Constants.PREFERENCES_KEY_HIDE_NOTIFICATION_BY_USER, hiddenByUser);
        editor.commit();
    }

    private static boolean isNotificationHiddenByUser(Context context) {
        SharedPreferences preferences = Util.getPreferences(context);
        return preferences.getBoolean(Constants.PREFERENCES_KEY_HIDE_NOTIFICATION_BY_USER, false);
    }

    private static Notification createSimpleNotification(Context context, MediaElement.Entry song) {
        Bitmap albumArt;
        try {
            albumArt = FileUtil.getAlbumArtBitmap(context, song, (int) Util.convertDpToPixel(64.0F, context));
            if (albumArt == null) {
                albumArt = Util.decodeBitmap(context, R.drawable.unknown_album);
            }
        } catch (Exception x) {
            Log.w(TAG, "Failed to get notification cover art", x);
            albumArt = Util.decodeBitmap(context, R.drawable.unknown_album);
        }
        Intent notificationIntent = new Intent(context, DownloadActivity.class);
        return new NotificationCompat.Builder(context).setOngoing(true)
                .setSmallIcon(R.drawable.stat_notify_playing)
                .setContentTitle(song.getTitle())
                .setContentText(song.getArtist())
                .setContentIntent(PendingIntent.getActivity(context, 0, notificationIntent, 0))
                .setLargeIcon(albumArt)
                .build();
    }

	@SuppressLint("NewApi")
	private static Notification createCustomNotification(Context context, MediaElement.Entry song, boolean playing) {

        Bitmap albumArt;
        try {
            albumArt = FileUtil.getUnscaledAlbumArtBitmap(context, song);
            if (albumArt == null) {
                albumArt = Util.decodeBitmap(context, R.drawable.unknown_album_large);
            }
        } catch (Exception x) {
            Log.w(TAG, "Failed to get notification cover art", x);
            albumArt = Util.decodeBitmap(context, R.drawable.unknown_album_large);
        }

        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
        contentView.setTextViewText(R.id.notification_title, song.getTitle());
        contentView.setTextViewText(R.id.notification_artist, song.getArtist());
        contentView.setImageViewBitmap(R.id.notification_image, albumArt);
        contentView.setImageViewResource(R.id.notification_playpause, playing ? R.drawable.media_pause : R.drawable.media_start);

        Intent intent = new Intent("1");
        intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
        intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
        contentView.setOnClickPendingIntent(R.id.notification_playpause, PendingIntent.getService(context, 0, intent, 0));

        intent = new Intent("2");
        intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
        intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
        contentView.setOnClickPendingIntent(R.id.notification_next, PendingIntent.getService(context, 0, intent, 0));

        intent = new Intent("4");
        intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
        intent.putExtra(Constants.INTENT_EXTRA_NAME_HIDE_NOTIFICATION, true);
        contentView.setOnClickPendingIntent(R.id.notification_close, PendingIntent.getService(context, 0, intent, 0));

        Intent notificationIntent = new Intent(context, DownloadActivity.class);

        Notification notification = new NotificationCompat.Builder(context)
                .setOngoing(true)
                .setSmallIcon(R.drawable.stat_notify_playing)
                .setContent(contentView)
                .setContentIntent(PendingIntent.getActivity(context, 0, notificationIntent, 0))
                .build();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        {
            notification.bigContentView = createBigContentView(context, song, albumArt, playing);
        }
        return notification;
    }

    private static RemoteViews createBigContentView(Context context, MediaElement.Entry song, Bitmap albumArt, boolean playing) {

        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification_expanded);
        contentView.setTextViewText(R.id.notification_title, song.getTitle());
        contentView.setTextViewText(R.id.notification_artist, song.getArtist());
        contentView.setTextViewText(R.id.notification_album, song.getAlbum());
        contentView.setImageViewBitmap(R.id.notification_image, albumArt);
        contentView.setImageViewResource(R.id.notification_playpause, playing ? R.drawable.media_pause : R.drawable.media_start);

        Intent intent = new Intent("1");
        intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
        intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
        contentView.setOnClickPendingIntent(R.id.notification_playpause, PendingIntent.getService(context, 0, intent, 0));

        intent = new Intent("2");
        intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
        intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
        contentView.setOnClickPendingIntent(R.id.notification_next, PendingIntent.getService(context, 0, intent, 0));

        intent = new Intent("3");
        intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
        intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS));
        contentView.setOnClickPendingIntent(R.id.notification_prev, PendingIntent.getService(context, 0, intent, 0));

        intent = new Intent("4");
        intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
        intent.putExtra(Constants.INTENT_EXTRA_NAME_HIDE_NOTIFICATION, true);
        contentView.setOnClickPendingIntent(R.id.notification_close, PendingIntent.getService(context, 0, intent, 0));

        return contentView;
    }

    private static boolean useSimpleNotification() {
        return Build.VERSION.SDK_INT < 11;
    }
}
